<?php
session_start();
include("db_connect.php"); 
connect();
$sql="SET CHARACTER SET UTF8";   
query($sql);

if($_POST['action']=='add')
{
	if($_POST['font']=="" or $_POST['font']<>$_SESSION['security_code'])
	{
		echo '<script>alert("invalid security code");history.back();</script>';	
		exit();
	}
	$data = array(
		"reference"=>$_POST['reference'],
		"username"=>'',
		"password"=>'',
		"name"=>$_POST['name'],
		"phone"=>$_POST['phone'],
		"tel2"=>$_POST['tel2'],
		"home"=>$_POST['home'],
		"fax"=>$_POST['fax'],
		"email"=>$_POST['email'],
		"province"=>$_POST['province'],
		"comment"=>$_POST['comment'],
		"date_regis"=>date("Y-m-d"),	
		"address"=>$_POST['address'],
		"pay_type"=>$_POST['pay_type'],
		"reference_name"=>$_POST['reference_name']
	);
	insert("tb_membership",$data);
	$insert_id = mysql_insert_id();
	
	//## เพิ่มข้อมูลลงเครือข่ายผู้แนะนำ ##
//	$data = array('up_id'=>$_POST['reference'],'down_id'=>$insert_id,'level'=>1);
//	insert('tb_benefi',$data);
//	## เพิ่มลูกข่ายให้กับ upline ของผู้แนะนำ ##
//	$sql = 'select * from tb_benefi where down_id = "'.$_POST['reference'].'" orderby level asc';
//	$qr = select($sql);
//	$total = count($qr);
//	$i=0;
//	while($i<$total):
//		$rs = $qr[$i];
//		$i++;
//		$level = $rs['level'] + 1;
//		$data = array('up_id'=>$rs['id'],'down_id'=>$insert_id,'level'=>$level);
//		insert('tb_benefi',$data);
//	endwhile;
	
	## บัตรประชาชน ##
	$fileupload=$_FILES['fileField']['tmp_name'];
	$fileupload_name=$_FILES['fileField']['name'];
	$fileupload_size=$_FILES['fileField']['size'];
	$fileupload_type=$_FILES['fileField']['type'];
	if($fileupload)
	{
		$array_last=explode(".",$fileupload_name);
		$c=count($array_last)-1; 
		$lastname=strtolower($array_last[$c]) ;
		$filename=date("YmdHis")."_".$insert_id.".".$lastname;
		if(move_uploaded_file($fileupload,"upload/$filename")){
			$data = array(
				"document"=>$filename
			);
			update("tb_membership",$data,"id = '".$insert_id."'");
		}
			
	}
	## สำเนาบัญชี ##
	$fileupload=$_FILES['book']['tmp_name'];
	$fileupload_name=$_FILES['book']['name'];
	$fileupload_size=$_FILES['book']['size'];
	$fileupload_type=$_FILES['book']['type'];
	if($fileupload)
	{
		$array_last=explode(".",$fileupload_name);
		$c=count($array_last)-1; 
		$lastname=strtolower($array_last[$c]) ;
		$filename=date("YmdHis")."_".$insert_id.".".$lastname;
		if(move_uploaded_file($fileupload,"upload/$filename")){
			$data = array(
				"book"=>$filename
			);
			update("tb_membership",$data,"id = '".$insert_id."'");
		}
			
	}
	## สำเนากรมธรรม์ ##
	$fileupload=$_FILES['insure']['tmp_name'];
	$fileupload_name=$_FILES['insure']['name'];
	$fileupload_size=$_FILES['insure']['size'];
	$fileupload_type=$_FILES['insure']['type'];
	if($fileupload)
	{
		$array_last=explode(".",$fileupload_name);
		$c=count($array_last)-1; 
		$lastname=strtolower($array_last[$c]) ;
		$filename=date("YmdHis")."_".$insert_id.".".$lastname;
		if(move_uploaded_file($fileupload,"upload/$filename")){
			$data = array(
				"insure"=>$filename
			);
			update("tb_membership",$data,"id = '".$insert_id."'");
		}
			
	}
	## เอกสารชำระเงิน ##
	$fileupload=$_FILES['pay']['tmp_name'];
	$fileupload_name=$_FILES['pay']['name'];
	$fileupload_size=$_FILES['pay']['size'];
	$fileupload_type=$_FILES['pay']['type'];
	if($fileupload)
	{
		$array_last=explode(".",$fileupload_name);
		$c=count($array_last)-1; 
		$lastname=strtolower($array_last[$c]) ;
		$filename=date("YmdHis")."_".$insert_id.".".$lastname;
		if(move_uploaded_file($fileupload,"upload/$filename")){
			$data = array(
				"pay"=>$filename
			);
			update("tb_membership",$data,"id = '".$insert_id."'");
		}
			
	}
	
	$to_name ="";
	$from_name	="Central Broker";
	$email_user_send ="central.insure.broker@gmail.com";
	$email_pass_send ="showidea";
	$reply_email = $_POST['email'];
	$reply_name = $_POST['name'];
				
	$subject = "ต้องการสมัครสมาชิก จากคุณ ".$_POST['name'];
	$body_text = "";
	$body_html ="
ชื่อ :: ".$_POST['name']."
			
เบอร์โทรติดต่อ :: ".$_POST['phone']."
			     
Email ::  ".$_POST['email']."	
			
ข้อความถึงบริษัท :: ".$_POST['comment']."
";
				
							  
			$sql="SELECT * FROM tb_admin where active =1 ";
			$qr = select($sql);
			$i=0;
			while($i<count($qr))
			{
				$rs = $qr[$i];
				$i++;
				$to_email=$rs[email];
				scriptdd_sendmail($to_name,$to_email,$from_name,$email_user_send,$email_pass_send,$subject,$body_html,$body_text,$reply_email,$reply_name); 
			} 
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
			echo "<script>alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ กรุณารอการติดต่อกลับจากเจ้าหน้าที่');window.location='index.php';</script>";
}
?>